<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            top: 0;
            width: 50px;
            height: 50px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div></div>
   <h1>1</h1>
    <button class="b1">800</button>

    <button class="b2">500</button>
    <script>
        var div = document.querySelector('div')
    
        var b1 = document.querySelector('.b1')
        var b2 = document.querySelector('.b2')
        b1.addEventListener('click',function(){
            animates(div, 800);    
        })
        b2.addEventListener('click',function(){
            animates(div, 500);    
        })
      

     
        function animates(obj, target,callback) {
            clearInterval(obj.tame)
            obj.tame= setInterval(function () {
                var step = (target - obj.offsetLeft) / 10
                step = step > 0 ? Math.ceil(step) : Math.floor(step)
                if (obj.offsetLeft == target) {
                    clearInterval(obj.tame)
                    callback && callback()
                }
                obj.style.left = obj.offsetLeft + step + 'px'
            }, 15)
        }

    </script>
</body>

</html>